package com.cheer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.MallAddressDao;
import com.cheer.dto.MallAddressAddDTO;
import com.cheer.dto.MallAddressUpdateDTO;
import com.cheer.entity.MallAddressEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.MallAddressService;
import com.cheer.vo.MallAddressInfoVO;
import com.cheer.vo.MallAddressListVO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("mallAddressService")
public class MallAddressServiceImpl extends ServiceImpl<MallAddressDao, MallAddressEntity> implements MallAddressService {

    @Autowired
    private MallAddressDao mallAddressDao;

    @Override
    public List<MallAddressListVO> listByUserId(Integer userId) {
        List<MallAddressEntity> addressEntityList = mallAddressDao.selectListByUserId(userId);

        return addressEntityList.stream()
                .map(this::convert)
                .collect(Collectors.toList());
    }

    @Override
    public Long add(MallAddressAddDTO addressAddDTO, Integer userId) {
        if (addressAddDTO.getIsDefault() == 1) {
            MallAddressEntity updateAddress = new MallAddressEntity()
                    .setIsDefault(0);
            mallAddressDao.updateByUserId(userId, updateAddress);
        }

        MallAddressEntity addressEntity = new MallAddressEntity();
        BeanUtil.copyProperties(addressAddDTO, addressEntity);
        addressEntity.setUserId(userId);

        mallAddressDao.insert(addressEntity);

        return addressEntity.getId();
    }

    @Override
    public MallAddressInfoVO info(Long id) {
        MallAddressEntity address = mallAddressDao.selectById(id);
        if (address == null) {
            throw new ServiceException("地址不存在");
        }

        MallAddressInfoVO addressInfoVO = new MallAddressInfoVO();
        BeanUtil.copyProperties(address, addressInfoVO);

        return addressInfoVO;
    }

    @Override
    public void update(MallAddressUpdateDTO addressUpdateDTO, Integer userId) {
        MallAddressEntity address = mallAddressDao.selectById(addressUpdateDTO.getId());
        if (address == null) {
            throw new ServiceException("地址不存在");
        }

        if (addressUpdateDTO.getIsDefault() == 1 && address.getIsDefault() == 0) {
            MallAddressEntity updateAddress = new MallAddressEntity()
                    .setIsDefault(0);
            mallAddressDao.updateByUserId(userId, updateAddress);
        }

        MallAddressEntity updateAddress = new MallAddressEntity();
        BeanUtil.copyProperties(addressUpdateDTO, updateAddress);

        mallAddressDao.updateById(updateAddress);
    }

    @Override
    public MallAddressListVO getDefault(Integer userId) {
        MallAddressEntity address = mallAddressDao.selectOneDefaultByUserId(userId);
        if (address == null) {
            return null;
        }

        return convert(address);
    }

    @Override
    public MallAddressListVO listById(Long addressId) {
        MallAddressEntity address = mallAddressDao.selectById(addressId);
        if (address == null) {
            return null;
        }

        return convert(address);
    }

    private MallAddressListVO convert(MallAddressEntity address) {
        MallAddressListVO addressListVO = new MallAddressListVO()
                .setId(address.getId())
                .setReceiver(address.getReceiver())
                .setPhone(address.getPhone())
                .setIsDefault(address.getIsDefault());

        if (StringUtils.equals(address.getProvince(), address.getCity())) {
            addressListVO.setAddress(StringUtils.join(Lists.newArrayList(address.getCity(),
                    address.getRegion(), address.getAddress()), " "));
        } else {
            addressListVO.setAddress(StringUtils.join(Lists.newArrayList(address.getProvince(), address.getCity(),
                    address.getRegion(), address.getAddress()), " "));
        }

        return addressListVO;
    }

}